<template>
  <el-dialog :visible.sync="showPointDialog" :before-close="check" title="平台名称：联通" width="75%">
    <el-card class="box-card">
      <div slot="header">
        <span class="bold">认证策略</span>
      </div>
      <el-row>
        <el-col :span="24">
          <div class="watch required">
            <span>未查询到用户时是否拦截：</span>
            <el-switch
              v-model="auth_fail_intercept"
              active-text="是"
              inactive-text="否"
            />
          </div>
        </el-col>
        <el-col v-show="!auth_fail_intercept" :span="24">
          <div class="watch required">
            <span>未查询到用户时是否自采：</span>
            <el-switch
              v-model="auth_fail_insertuser"
              active-text="是"
              inactive-text="否"
            />
          </div>
        </el-col>
        <el-col :span="24">
          <div class="watch required">
            <span>账号认证规则：</span>
            <el-radio-group v-model="aaa_auth_role">
              <el-radio border size="small" :label="1">仅使用“账号”认证</el-radio>
              <el-radio border size="small" :label="2">使用“账号+密码”认证</el-radio>
            </el-radio-group>
          </div>
        </el-col>
        <el-col :span="24">
          <div class="watch required">
            <span>是否校验MAC地址绑定：</span>
            <el-radio-group v-model="check_mac">
              <el-radio border size="small" :label="1">校验MAC地址绑定</el-radio>
              <el-radio border size="small" :label="2">不校验MAC地址绑定，只做记录</el-radio>
              <el-radio border size="small" :label="3">不校验MAC地址绑定，并将新MAC增加录入绑定关系</el-radio>
            </el-radio-group>
          </div>
        </el-col>
        <el-col :span="24">
          <div class="watch required">
            <span>是否校验MAC地址合法性：</span>
            <el-switch
              v-model="check_mac_legal"
              active-text="是"
              inactive-text="否"
            />
          </div>
        </el-col>
        <el-col :span="24">
          <div class="watch required">
            <span>认证频率配置：</span>
            <el-radio-group v-model="auth_rate" @change="getStatus">
              <el-radio border size="small" :label="1">每次开机认证</el-radio>
              <el-radio border size="small" :label="2">每次访问首页认证</el-radio>
              <el-radio border size="small" :label="3">每个EPG页面认证</el-radio>
              <el-radio border size="small" :label="4">
                <el-popover
                  v-model="visibles"
                  placement="right"
                  width="178"
                  trigger="manual"
                >
                  <div class="propp">
                    心跳间隔：
                    <el-input
                      v-model="auth_timer"
                      size="mini"
                      style="width:40px;height:23px;line-height:23px;"
                    />
                    &nbsp;分钟
                  </div>
                  <span slot="reference">心跳激活</span>
                </el-popover>
              <!-- <el-radio border size="small" :label="4">心跳认证</el-radio> -->
              </el-radio>
            </el-radio-group>
          </div>
        </el-col>
        <el-col :span="24">
          <div class="watch required">
            <span>TempToken是否失效：</span>
            <el-switch
              v-model="ethier"
              active-text="是"
              inactive-text="否"
            />
          </div>
        </el-col>
        <el-col v-if="ethier" :span="24">
          <div class="watch required">
            <span>TempToken失效间隔：</span>
            <el-input
              v-model="timetoken_timeout"
              size="mini"
              style="width:80px"
            />&nbsp;分钟
          </div>
        </el-col>
        <el-col :span="24">
          <div class="watch required">
            <span>认证查询数据库层级：</span>
            <el-radio-group v-model="dataBase">
              <el-radio border size="small" :label="1">查询Redis</el-radio>
              <el-radio border size="small" :label="2">查询Redis→mangoDB</el-radio>
              <el-radio border size="small" :label="3">查询Redis→mangoDB→MySQL</el-radio>
            </el-radio-group>
          </div>
        </el-col>
      </el-row>
    </el-card>
    <el-card class="box-card1">
      <div slot="header">
        <span class="bold">鉴权策略</span>
      </div>
      <el-col :span="24">
        <div class="watch required">
          <span>对免费节目是否鉴权：</span>
          <el-switch
            v-model="free_program_auth"
            active-text="是"
            inactive-text="否"
          />
        </div>
      </el-col>
    </el-card>
    <div class="btnall">
      <el-button
        type="primary"
        round
        size="mini"
        style="width:100px"
        @click="save"
      >保存</el-button>
      <el-button
        type="primary"
        plain
        round
        size="mini"
        style="width:100px"
        @click="check"
      >取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { plusNum } from '@/utils/validate.js'
import { search, save } from '@/api/index.js'
export default {
  name: 'Watch',
  props: {
    dialogStatus: {
      type: Boolean,
      default: false
    },
    platformId: {
      type: String,
      default: '0'
    }
  },
  data() {
    return {
      showPointDialog: false,
      ethier: true,
      platform: '',
      auth_timer: '',
      auth_fail_insertuser: true, // 是否自采
      check_mac: 1, // mac地址绑定
      aaa_auth_role: 1, // 账号认证规则
      auth_fail_intercept: true, // 是否拦截
      check_mac_legal: true, // max地址合法性
      auth_rate: 1, // 认证频率
      dataBase: 1, // 数据库层级
      free_program_auth: true, // 是否免费给
      timetoken_timeout: '', // 失效时间
      visibles: false // 是否显示心跳时间
    }
  },
  watch: {
    dialogStatus(newVal) {
      this.showPointDialog = newVal
      this.platform = this.platformId
      if (newVal) {
        this.loadData(this.platform)
      }
    }
  },
  methods: {
    loadData(platform) {
      var data = {
        platformId: platform
      }
      search(data).then(val => {
        const res = val.data
        var mongodb_switch = res.mongodb_switch // 应用mongodb
        var redis_switch = res.redis_switch // redis
        var mysql_switch = res.mysql_switch
        if (mongodb_switch === '1' && mysql_switch === '1' && redis_switch === '0') {
          this.dataBase = 1
        }
        if (mongodb_switch === '0' && redis_switch === '0' && mysql_switch === '1') {
          this.dataBase = 2
        }
        if (mongodb_switch === '0' && redis_switch === '0' && mysql_switch === '0') {
          this.dataBase = 3
        }

        // 拦截
        var auth_fail_intercept = res.auth_fail_intercept
        if (auth_fail_intercept === '0') {
          this.auth_fail_intercept = true
        }
        if (auth_fail_intercept === '1') {
          this.auth_fail_intercept = false
        }

        // 自采
        var auth_fail_insertuser = res.auth_fail_insertuser
        if (auth_fail_insertuser === '0') {
          this.auth_fail_insertuser = true
        }
        if (auth_fail_insertuser === '1') {
          this.auth_fail_insertuser = false
        }

        // mac地址绑定
        var check_mac = res.check_mac
        if (check_mac === '0') {
          this.check_mac = 1
        }
        if (check_mac === '1') {
          this.check_mac = 2
        }

        if (check_mac === '2') {
          this.check_mac = 3
        }

        // 认证规则
        var aaa_auth_role = res.aaa_auth_role
        if (aaa_auth_role === '0') {
          this.aaa_auth_role = 1
        }
        if (aaa_auth_role === '1') {
          this.aaa_auth_role = 2
        }

        // 合法性
        var check_mac_legal = res.check_mac_legal
        if (check_mac_legal === '0') {
          this.check_mac_legal = true
        }
        if (check_mac_legal === '1') {
          this.check_mac_legal = false
        }

        // 认证频率
        var auth_rate = res.auth_rate
        if (auth_rate === '0') {
          this.auth_rate = 1
        }
        if (auth_rate === '1') {
          this.auth_rate = 2
        }
        if (auth_rate === '2') {
          this.auth_rate = 3
        }
        if (auth_rate === '3') {
          this.auth_rate = 4
          this.visibles = true
        }

        // 间隔
        this.auth_timer = res.auth_timer

        // token失效
        this.timetoken_timeout = res.timetoken_timeout
        if (this.timetoken_timeout) {
          this.ethier = true
        } else {
          this.ethier = false
        }
        console.log(res)
        // 免费节目播放鉴权
        var free_program_auth = res.free_program_auth
        if (free_program_auth === '0') {
          this.free_program_auth = true
        }
        if (free_program_auth === '1') {
          this.free_program_auth = false
        }
      })
    },
    getStatus() {
      if (this.auth_rate === 4) {
        this.visibles = true
      } else {
        this.visibles = false
        this.auth_timer = ''
      }
    },
    save() {
      if (this.ethier && !plusNum(this.timetoken_timeout)) {
        this.$message({
          type: 'error',
          message: '分钟必须为数字'
        })
        return false
      }
      if (this.auth_rate === 4 && !plusNum(this.auth_timer)) {
        this.$message({
          type: 'error',
          message: '分钟必须为数字'
        })
        return false
      }
      if (this.dataBase === 1) {
        this.redis_switch = '0'
        this.mongodb_switch = '1'
        this.mysql_switch = '1'
      }
      if (this.dataBase === 2) {
        this.redis_switch = '0'
        this.mongodb_switch = '0'
        this.mysql_switch = '1'
      }
      if (this.dataBase === 3) {
        this.redis_switch = '0'
        this.mongodb_switch = '0'
        this.mysql_switch = '0'
      }
      var aaa_auth_role
      if (this.aaa_auth_role === 1) {
        aaa_auth_role = '0'
      }
      if (this.aaa_auth_role === 2) {
        aaa_auth_role = '1'
      }

      var auth_fail_insertuser
      if (this.auth_fail_insertuser) {
        auth_fail_insertuser = '0'
      }
      if (!auth_fail_insertuser) {
        auth_fail_insertuser = '1'
      }

      var auth_fail_intercept
      if (this.auth_fail_intercept) {
        auth_fail_intercept = '0'
        auth_fail_insertuser = '1'
      }
      if (!this.auth_fail_intercept) {
        auth_fail_intercept = '1'
      }
      var check_mac
      if (this.check_mac === 1) {
        check_mac = '0'
      }
      if (this.check_mac === 2) {
        check_mac = '1'
      }
      if (this.check_mac === 3) {
        check_mac = '2'
      }
      var check_mac_legal
      if (this.check_mac_legal) {
        check_mac_legal = '0'
      }
      if (!this.check_mac_legal) {
        check_mac_legal = '1'
      }
      var auth_rate
      if (this.auth_rate === 1) {
        auth_rate = '0'
      }
      if (this.auth_rate === 2) {
        auth_rate = '1'
      }
      if (this.auth_rate === 3) {
        auth_rate = '2'
      }
      if (this.auth_rate === 4) {
        auth_rate = '3'
      }
      var auth_timer = this.auth_timer ? this.auth_timer : null
      var timetoken_timeout = this.ethier ? this.timetoken_timeout : ''
      var free_program_auth
      if (this.free_program_auth) {
        free_program_auth = '0'
      }
      if (!this.free_program_auth) {
        free_program_auth = '1'
      }
      var data = {
        platformId: this.platform,
        mongodb_switch: this.mongodb_switch,
        redis_switch: this.redis_switch,
        mysql_switch: this.mysql_switch,
        auth_fail_intercept: auth_fail_intercept,
        auth_fail_insertuser: auth_fail_insertuser,
        check_mac: check_mac,
        check_mac_legal: check_mac_legal,
        auth_rate: auth_rate,
        auth_timer: auth_timer,
        aaa_auth_role: aaa_auth_role,
        timetoken_timeout: timetoken_timeout,
        free_program_auth: free_program_auth

      }
      console.log(data)
      save(data).then(res => {
        console.log(data)
        if (res.resultCode === '0000') {
          this.$message({
            type: 'success',
            message: '保存成功'
          })
          this.check()
        }
      })
      // this.check()
    },
    // 回调父组件
    check() {
      this.visibles = false
      this.showPointDialog = false
      this.$emit('closeDia')
      console.log(this.visibles)
      this.ethier = true
      this.platform = ''
      this.auth_timer = ''
      this.auth_fail_insertuser = true // 是否自采
      this.check_mac = 1 // mac地址绑定
      this.aaa_auth_role = 1 // 账号认证规则
      this.auth_fail_intercept = true // 是否拦截
      this.check_mac_legal = true // max地址合法性
      this.auth_rate = 1 // 认证频率
      this.dataBase = 1 // 数据库层级
      this.free_program_auth = true // 是否免费给
      this.timetoken_timeout = '' // 失效时间
      this.visibles = false // 是否显示心跳时间
    }
  }
}
</script>

<style scoped>
.watch{
  margin: 10px 0;
}
.watch span{
  font-weight: bold;
}
.btnall{
  margin: 17px auto -12px auto;
  width: 230px;
}
.required::before{
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
.box-card{
  margin-top: -25px;
}
.box-card1{
  margin-top: 10px;
}
.box-card2{
  margin-top: 10px;
}
.watch{
  margin: 10px 0;
}
.bold{
  font-weight: bold;
}
.el-card__header{
  padding: 9px 20px !important;
}
.required::before{
  content: "*";
  color: #f56c6c;
  margin-right: 4px;
}
.watch{
  margin: 12px 0;
}
</style>
<style>
.propp input{
  padding: 0 6px !important;
  text-align: center;
}
.el-radio.is-bordered+.el-radio.is-bordered{
  margin-left: 0 !important;
}
.el-radio{
  margin-right: 18px !important;
}
.el-popper[x-placement^=right]{
  margin-left: 22px !important;
}
</style>
